﻿///***************************************************************
/// 시  스  템 : Updms.Prj
/// 단위시스템 : PRJ
/// 계      층 : Presentation Layer
/// 프로그램명 : Prj3010p
/// 파  일  명 : Prj3010p.aspx.cs
/// 설      명 : 이메일보고
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-12-24 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using SMFramework.Adapter;
using SMFramework.Configuration;
using SMFramework.Data;
using SMFramework.Utility;
using SMFramework.Web.UI.WebControls.SMGridView;

using Base.AppFramework.Web.UI.CommandBar;
using Base.Dac;

using Updms.Prj.Exe;


namespace Updms.Prj
{
	public partial class Prj3010p : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Prj3010p";
		public static readonly string gs_path_info = "/updms/Prj/" + PID + ".aspx";

		Global global = new Global();

		//검색
		protected string kw1 = "";	//Seq

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
				SearchProcess();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion
		
		#region SetSearchParameter

		//검색파라미터
		private void SetSearchParameter()
		{
			if (String.IsNullOrEmpty(Request.QueryString["kw1"]))	// url에 값이 없으면
			{
				kw1 = "";
			}
			else
			{
				kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
			}
		}		

		#endregion

		#region 디테일 Method (DoSendMail)

		private void DoSendMail(DataPack lo_dp)
		{
			DataPack lo_dp_set = new DataPack();
			
			try
			{
				MailClient mc = new MailClient(WebConfig.GetValue(WebConfig.Updms, "Mail.Host").ToString());

				string ls_rslt = mc.SendMail(lo_dp);

				if (ls_rslt == "OK")
				{
					using (Prj3010p_RT _Prj3010p_RT = new Prj3010p_RT())
					{
						lo_dp_set.AddProperty("Seq", typeof(string), lbl_pk.Text);
						lo_dp_set.AddProperty("Update_User", typeof(string), USER_ID);

						int li_return = _Prj3010p_RT.SetSendYn(lo_dp_set);
						CallMsgBoxInAjax("MSG_INF_009");	//메일을 전송했습니다
					}
				}
				else
				{
					CallMsgBoxInAjax("MSG_ERR_135");	//메일전송에 실패했습니다
				}
			}
			catch (Exception ex)
			{
				CallExceptionMsgBoxInAjax(ex);
			}
		}

		#endregion

		#region CRUD (SelectProcess, SetReportInfoForEmailDetail, SearchProcess)

		//조회
		private void SelectProcess()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			if (kw1 == "")
			{
				CallMsgBoxInAjax("MSG_ERR_134");	//전송할 내용을 가져오지 못했습니다.
			}
			else
			{
				//검색
				lo_dp.AddProperty("li_seq", typeof(string), kw1);
				lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);
				
				//전송후 DB업데이트용
				lbl_pk.Text = kw1;

				using (Prj3010p_NT _Prj3010p_NT = new Prj3010p_NT())
				{
					lo_ds.Merge(_Prj3010p_NT.GetReportInfoForEmail(lo_dp));
				}
				
				txt_from.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_PRJ_REPORT_MGT", "From");
				txt_to.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_PRJ_REPORT_MGT", "To");
				txt_subject.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_PRJ_REPORT_MGT", "Title");

				string ls_body = DataSetUtil.GetFldStr(lo_ds, "UPDMS_PRJ_REPORT_MGT", "Results") + "\r\n\r\n" + DataSetUtil.GetFldStr(lo_ds, "UPDMS_PRJ_REPORT_MGT", "Plans");
				txt_body.Text = ls_body;
				
				//상세조회
				string ls_start_dt = DataSetUtil.GetFldStr(lo_ds, "UPDMS_PRJ_REPORT_MGT", "Start_Dt");
				string ls_end_dt = DataSetUtil.GetFldStr(lo_ds, "UPDMS_PRJ_REPORT_MGT", "End_Dt");

				SetReportInfoForEmailDetail(ls_start_dt, ls_end_dt);
			}
		}
		
		//상세조회
		private void SetReportInfoForEmailDetail(string ls_start_dt, string ls_end_dt)
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			lo_dp.AddProperty("ls_start_dt", typeof(string), ls_start_dt);
			lo_dp.AddProperty("ls_end_dt", typeof(string), ls_end_dt);

			using (Prj3010p_NT _Prj3010p_NT = new Prj3010p_NT())
			{
				lo_ds.Merge(_Prj3010p_NT.GetReportInfoForEmailDetail(lo_dp));
			}

			GridView1.DataSource = lo_ds;
			GridView1.DataBind();
		}
		
		private void SearchProcess()
		{
			SelectProcess();
		}

		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_POPUP);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
					//사용권한별 버튼처리
					PagePreRender.SetVisibleCtrl(this, access.CheckPgmUsingGrant(USER_DIV, PID), access.GetVisibleCtrl(PID));
				}
			}
			ChangePageMode();
		}

		//ChangePageMode
		private void ChangePageMode()
		{
			SetGridViewHeader(global.GetColumnListByPgmId(PID, LANG_SET));
		}
		
		#endregion

		#region GridView이벤트

		//RowDataBound
		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				//마우스올렸을 때
				e.Row.Attributes.Add("onMouseOver", GRID_ONMOUSEOVER_COLOR);
				e.Row.Attributes.Add("onMouseOut", GRID_ONMOUSEOUT_COLOR);

				//번호
				e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
			}

			//머지
			SMGridView sGv = new SMGridView(this.Page, ref GridView1);
			sGv.RowSpanGridView(GridView1, 1);	//1.프로젝트명
		}
		
		#endregion

		#region 버튼이벤트

		//검색
		protected void ibtn_send_Click(object sender, EventArgs e)
		{
			DataPack lo_dp = new DataPack();
			lo_dp.AddProperty("From", typeof(string), txt_from.Text.Trim());
			lo_dp.AddProperty("To", typeof(string), txt_to.Text.Trim());
			lo_dp.AddProperty("Subject", typeof(string), txt_subject.Text.Trim());
			lo_dp.AddProperty("Body", typeof(string), txt_body.Text.Trim());
			lo_dp.AddProperty("IsBodyHtml", typeof(string), rbl_is_html.SelectedValue);

			DoSendMail(lo_dp);
		}
		
		#endregion


	}
}
